//
//  HESecurtyChainData.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/2/22.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HESecurtyChainData.h"
#import "HEKeyChain.h"

#define KEYCHAIN_KEY  [NSString stringWithFormat:@"%@-password_slider",[UserManager sharedUserManager].curUserInfo.username]

#define FINGERPRINT_KEYCHAIN_KEY  [NSString stringWithFormat:@"%@-password_fingerprint",[UserManager sharedUserManager].curUserInfo.username]


@implementation HESecurtyChainData

+ (BOOL)isSave
{ 
    NSString *str = [HEKeyChain objectForKey:KEYCHAIN_KEY];
    if (str && str.length>0 && [str isKindOfClass:[NSString class]]) {
        return YES;
    }
    
    return NO;
}

+ (BOOL)isFirstInput:(NSString *)str
{
    NSString *oldStr = [HEKeyChain objectForKey:KEYCHAIN_KEY];
    if (oldStr && oldStr.length>0 && [oldStr isKindOfClass:[NSString class]])
    {
        return NO;
    }
    
    [HEKeyChain setObject:str forKey:KEYCHAIN_KEY];
    
    return YES;
}

+ (BOOL)isSecondInputRight:(NSString *)str
{
    NSString *oldStr = [HEKeyChain objectForKey:KEYCHAIN_KEY];
  
    if ([oldStr isKindOfClass:[NSNull class]]) {
        return NO;
    }
    else if (!oldStr || oldStr.length<1 || ![oldStr isKindOfClass:[NSString class]]) {
        return NO;
    }
    else if (oldStr.length==str.length  &&  [oldStr isEqualToString:str]) {
        return YES;
    }
    
    return NO;
}

+ (void)forgotPsw
{
    [HEKeyChain removeObjectForKey:KEYCHAIN_KEY];
}

+ (void)setPSW:(NSString *)str
{
    [HEKeyChain setObject:str forKey:KEYCHAIN_KEY];
}


#pragma mark - 指纹密码

+ (BOOL)hasFingerprint {
    NSString *str = [HEKeyChain objectForKey:FINGERPRINT_KEYCHAIN_KEY];
    if (str && str.length>0 && [str isKindOfClass:[NSString class]]) {
        return YES;
    }
    return NO;
}

/** 保存随机生成的指纹密码 ***/
+ (void)saveFingerprintPWD {
    
    UserInfo *user = [UserManager sharedUserManager].curUserInfo;
    HESecuryChainUserModel *saveUser = [HESecuryChainUserModel new];
    saveUser.username = user.username;
    saveUser.password = [DYTools createUuid];
    saveUser.avatar = user.avatar;
    
    [HEKeyChain setObject:[saveUser mj_JSONString] forKey:FINGERPRINT_KEYCHAIN_KEY];
}

+ (HESecuryChainUserModel *)getFingerprintPWD {
    NSString *oldStr = [HEKeyChain objectForKey:FINGERPRINT_KEYCHAIN_KEY];
    if ([DYTools isBlankString:oldStr]) {
        return nil;
    }
    HESecuryChainUserModel *saveUser = [HESecuryChainUserModel mj_objectWithKeyValues:[oldStr mj_JSONObject]];
    return saveUser;
}

+ (void)removeFingerprintPWD {
    [HEKeyChain removeObjectForKey:FINGERPRINT_KEYCHAIN_KEY];
}

@end



@implementation HESecuryChainUserModel

@end
